package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@WebServlet(urlPatterns = "/life", loadOnStartup = 1, initParams = {
        @WebInitParam(name = "key", value = "value"),
        @WebInitParam(name = "username", value = "admin"),
        @WebInitParam(name = "password", value = "123456")
})
/*
* 第一步创建对象*/
public class LifeServlet extends HttpServlet {
/*
* 第二部初始化方法*/
//    该方法之执行一次
    @Override
    public void init() throws ServletException {
        System.out.println("*******************LifeServlet.init*******************");
    }
/*
* 第三步执行核心业务逻辑*/
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("service方法可以处理所有请求方式");
        String key = getInitParameter("key");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("key:" + key + ",username:" + username + ",password:" + password);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("这是post请求");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("这是get请求");
    }
    /*
     * 第四步销毁*/
    @Override
    public void destroy() {
        System.out.println("*******************LifeServlet.destroy*******************");
    }
}
